package com.ys.controller.web.base;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.base.TbIncomePay;
import com.ys.entity.base.TbIncomePayItem;
import com.ys.service.base.TbIncomePayService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/base/tbIncomePay")
public class WebTbIncomePayController {


	@Autowired
	private CommonApiService<TbIncomePay> commonApiService;

	@Autowired
	private TbIncomePayService tbIncomePayService;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TB_IncomePay);
			TbIncomePay tbIncomePay = new TbIncomePay()
					.setStatus(1)
					.setBid(bid)
					.setCreateTime(new Date());

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TB_IncomePay, new ArrayList<Map<String, Object>>());

			commonApiService.toAdd(model, tbIncomePay, MenuId.TB_IncomePay, null, subGridConfig);
			return "base/tbIncomePay/tbIncomePayAdd";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			if (params.get("bid") == null){
				return W.error(model, "单号不能为空");
			}

			String bid = (params.get("bid").toString());

			Map entity = tbIncomePayService.getExtraParam(bid);
			if (entity == null){
				return W.error(model, "个税付款单不存在!");
			}
			List<Map<String, Object>> sonlist =tbIncomePayService.getItemList(bid);

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TB_IncomePay, sonlist);


			commonApiService.toEdit(model, Long.valueOf(entity.get("id").toString()), new TbIncomePay(), MenuId.TB_IncomePay, entity, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "base/tbIncomePay/tbIncomePayEdit";
	}

}
